package echarts

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type EchartsCourseController struct {
	beego.Controller
}

func (c *EchartsCourseController) Get() {

	c.TplName = "echarts/echarts-course.html"
}

func (c *EchartsCourseController) GetCourseChart() {

	o := orm.NewOrm()

	// 财务时间
	var caiwuDate orm.ParamsList
	o.Raw("select caiwu_date from sys_caiwu_data").ValuesFlat(&caiwuDate)

	var courseType orm.ParamsList = orm.ParamsList{
		"django",
		"vue_django",
		"celery",
	}

	// 数据库内课程列表
	var djangoList orm.ParamsList
	o.Raw("select django from sys_caiwu_data").ValuesFlat(&djangoList)
	var vueDjangoList orm.ParamsList
	o.Raw("select vue_django from sys_caiwu_data").ValuesFlat(&vueDjangoList)
	var celeryList orm.ParamsList
	o.Raw("select celery from sys_caiwu_data").ValuesFlat(&celeryList)

	// 初始化列表数据
	mapData := map[string]interface{}{}

	servicesDate := []map[string]interface{}{}

	courseMapping := map[string]interface{}{
		"django":     djangoList,
		"vue_django": vueDjangoList,
		"celery":     celeryList,
	}

	for i := 0; i < len(courseType); i++ {
		mapServices := map[string]interface{}{}
		mapServices["name"] = courseType[i]
		mapServices["type"] = "line"
		mapServices["stack"] = "总量"

		// 转换成string
		dataList := courseMapping[courseType[i].(string)]

		mapServices["data"] = dataList

		// 判断
		if courseType[i] == "django" {
			mapServices["data"] = djangoList
		}

		servicesDate = append(servicesDate, mapServices)
	}

	mapData["caiwu_date"] = caiwuDate
	mapData["course_type"] = courseType
	mapData["services_data"] = servicesDate

	c.Data["json"] = mapData
	c.ServeJSON()

}
